import { SystemBase } from "../core/system-base";

export class InputSystem implements SystemBase {
    private _keyStates: { [key: string]: boolean } = {}
    
    constructor(attachedElement: HTMLElement) {
        this.setupListenerToElement(attachedElement)
    }
    
    private setupListenerToElement(element: HTMLElement) {
        const keyDownCallback = (e: KeyboardEvent) => {
            this._keyStates[e.key] = true
        }
        
        const keyUpCallback = (e: KeyboardEvent) => {
            this._keyStates[e.key] = false
        }

        element.addEventListener('keydown', keyDownCallback)
        element.addEventListener('keyup', keyUpCallback)
    }

    getKeyState(key: string): boolean {
        return Boolean(this._keyStates[key])
    }

    getSystemName(): string {
        return 'Input System'
    }
}
